
// // 请求拦截器
// axios.interceptors.request.use( 
//     (config)=>{  
//         //设置请求头参数
//         config.headers['Authorization'] = 'Bearer' + ' ' + localStorage.getItem('token');
//         // config.headers['Authorization'] = 'Bearer ' + token
//         return config;
//     },
//     (error)=>{
//         return Promise.reject(error)
//     }
// )

// // 响应拦截器
// axios.interceptors.response.use( 
//     (res)=>{  
//         return res;
//     },
//     (error)=>{ 

//         if( error.response.status == 401 ){
//             localStorage.removeItem('token');
//             location.href = './index1.html';

//         }else if( error.response.status == 404 ){
//             alert('访问路径有误!');
//         }else if( error.response.status == 500 ){
//             alert('服务器内部错误!');
//         }

//         return Promise.reject(error)
//     }
// )



function render(data) {
    var str = ''
    str = data.map((item, index) => {
        return `<ul class="row">
        <li>${item.email}</li> 
        <li>${item.name}</li>
        <li>${item.phone}</li>
        <li>${item.ingroup}</li>
        <li>${item.isadmin ? '是' : '否'}</li>
        <li class="${item.status ? '' : 'error'}">${item.status ? '正常' : '异常'}</li>
        <li>
            <span class="iconfont icon-bianji editicon" id="101"></span>
            <span class="iconfont icon-shanchu deleteicon" data-phone="${item.phone}"></span>
                    </li>
                </ul>`
    }).join('')

    if (str == '') {
        str = '<ul style="text-align:center;color:red">未查询到数据!</ul>'
    }
    document.querySelector('.table .table-body').innerHTML = str;
}
var page = 1
function oList() {
    axios.get('/user/list', { params: { page: page, count: 2 } }).then((res) => {
        render(res.data.list)
        console.log(res.data);
    })
}
oList()

//下一页
var next = document.querySelector('.next')
next.onclick = function () {
    if (page <3) {
        page++
    }
    oList()
}
//上一页
var prev = document.querySelector('.prev')
prev.onclick = function () {
    if (page > 1) {
        page--
    }
    oList()
}
// 用来发送ajax请求的

axios.post('/user/login', { phone: '13611129070', pass: '1234' }).then((res) => {
    console.log(res.data);
})


//搜索
var searchBtn = document.querySelector('.searchbox .search');
//获取输入框
var inputEle = document.querySelector('.searchbox input');

searchBtn.onclick = () => {
    axios.get('/user/search', { params: { searchkey: inputEle.value } }).then((res) => {
        render(res.data.list)
        console.log(res.data);
    })
}

//删除
var tablebody = document.querySelector('.table-body');
tablebody.onclick = (e) => {
    if (e.target.tagName == 'SPAN' && e.target.classList.contains('icon-shanchu')) {
        axios.post('/user/delete', { phone: e.target.dataset.phone }).then((res) => {
            console.log(res.data);
            //让删除接口 返回删除后的数据,
            render(res.data.list)
        })
    }
}

//获取localStorage中存储的用户信息
var userinfo = JSON.parse( localStorage.getItem('userinfo') );
//渲染用户到首页
document.querySelector('.username').innerHTML = userinfo.name;

